/*
 * WebUtils.h
 *
 *  Created on: Jun 18, 2013
 *      Author: alexandru
 *      TO BE COMPILED using -lcurl
 */
#include <sstream>
#include "curl/curl.h"
#include "curl/easy.h"
#include "FileResult.h"

#ifndef WEBUTILS_H_
#define WEBUTILS_H_


int stn_webWriter(char *data, size_t size, size_t nmemb, string *buffer){
	//fprintf(stderr,"Hello I am a function pointer\n");
	int result = 0;
	if(buffer != NULL) {
		buffer -> append(data, size * nmemb);
		result = size * nmemb;
	}
	return result;
}

FileResult stn_readWebpage(string path){
	FileResult composedData;


	CURL *curl;
		CURLcode res;
		string buffer;		/* See: CURLOPT_WRITEDATA */


		curl=curl_easy_init();
			if(curl){
				//cout << "!!!Curl Easy init!!!" << endl; // prints !!!Hello World!!!


				curl_easy_setopt (curl, CURLOPT_URL, path.c_str());
				//curl_easy_setopt (curl, CURLOPT_URL, "http://stackoverflow.com/questions/1053099/how-can-i-get-content-of-web-page");
				curl_easy_setopt(curl, CURLOPT_HEADER, 0);	 /* No we don't need the Header of the web content. Set to 0 and curl ignores the first line */
				curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0); /* Don't follow anything else than the particular url requested*/
				curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, stn_webWriter);	/* Function Pointer "writer" manages the required buffer size */
				curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer ); /* Data Pointer &buffer stores downloaded web content */

				res = curl_easy_perform (curl);
				curl_easy_cleanup (curl);

				std::istringstream iss(buffer);
				string line, item;
				int linenum = 0;
				string allData="";

				while (getline (iss, line)){
				        linenum++;													/* Move to Next Line */
				        //cout << "\nLine #" << linenum << ":" << endl;				/* Terminal Printout */
				        std::istringstream linestream(line);						/* Read Next Line */
				        int itemnum = 0;
				        while (getline (linestream, item, ',')){
				            itemnum++;
				            allData+=item;
				            composedData.addLine(item);
				           // cout << "Item #" << itemnum << ": " << item << endl;	/* Terminal Printout */
				        } // End WHILE (items)

				    } //End WHILE (lines)
				return composedData;
			}
	return composedData;

}



#endif /* WEBUTILS_H_ */
